package data;

import java.sql.Connection;

import data.bean.DataBean;
import data.exception.DaoException;
import data.mysql.MySQLDaoFactory;

/**
 * Abstract DAO Factory: for the flexibility of changing persistence
 * implementation.
 * 
 * @author chuxiaoyuan
 * 
 */
public abstract class DaoFactory {

	/**
	 * Constructor.
	 */
	protected DaoFactory() {
		// No instance
	}

	/**
	 * Supported DAO types.
	 * 
	 * @author chuxiaoyuan
	 */
	public enum DaoType {
		MySQL; // MySQL relational database
	}

	/**
	 * Get DAO factory of the specified type.
	 * 
	 * @param type
	 * @return
	 */
	public static DaoFactory getDAOFactory(DaoType type) {

		switch (type) {
		case MySQL:
			return new MySQLDaoFactory();
		default:
			return null;
		}
	}

	/**
	 * Get the default implementation.
	 */
	public static DaoFactory getDaoFactory() {
		return getDAOFactory(DaoType.MySQL);
	}

	/**
	 * Supported DAO names.
	 * 
	 * @author chuxiaoyuan
	 */
	public enum DaoName {
		UserDao, 
		ProductprofileDao, 
		ProductDao, 
		OrderDao, 
		MenuitemDao, 
		CustomerDao, 
		CompositionDao, 
		ComponentDao;
	}

	/**
	 * Get the specified DAO instance.
	 * 
	 * @param daoName
	 *            DAO name
	 * @return the dao instance
	 */
	public abstract <T extends IDao<? extends DataBean>> T getDao(DaoName daoName);
	
	/**
	 * Get connection.
	 * 
	 * @return the new connection.
	 * @throws DaoException get connection failed.
	 */
	public abstract Connection getConnection() throws DaoException;
}
